﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace FrbaCommerce.Codigo_reutilizable
{
    class FuncionesBaseDatos
    {

        public static void CompletarDataGrid(DataGridView dgv1, SqlConnection conexion, SqlCommand cmd)
        {
            SqlDataAdapter da = new SqlDataAdapter { SelectCommand = cmd };
            DataTable tabla = new DataTable();
            da.Fill(tabla);
            conexion.Close();
            dgv1.DataSource = tabla;
            dgv1.Refresh();
            dgv1.ClearSelection();
        }

        public static SqlCommand crearProcedure(string nombre, SqlConnection conexion, params SqlParameter[] paramSP)
        {
            //Instancio un Stored Procedure
            SqlCommand sp = new SqlCommand(nombre, conexion) { CommandType = CommandType.StoredProcedure };
            //Agrego parametros al SP
            foreach (SqlParameter parametro in paramSP)
            {
                agregarParametro(parametro, sp);
            }

            return sp;
        }

        public static void agregarParametro(SqlParameter p, SqlCommand sp)
        {
            sp.Parameters.Add(p);
        }
        
    }
}